<?php

//在没有分类的情况下取文章
function nocat_quaricle($siteId,$num) {
	
	global $wpdb;
	$replacedHtml = "";
	$displayCount;
	//数据库模板
	$siteHtml;
	
	$sql_site = "select shownum,site_html from  bw_template_site where id=$siteId";
	$result_site = $wpdb->get_results ( $sql_site );
	foreach ( $result_site as $row_site ) {
		$displayCount = $row_site->shownum;
		$siteHtml = $row_site->site_html;
	}
	
	if ($num < 2) {
		$newposts = get_posts ( "orderby=post_date&numberposts=$displayCount" );
	} else if ($num == 3) {
		$newposts = get_posts ( "orderby=title&numberposts=$displayCount&order=ASC" );
	} else if ($num == 8) {
		$newposts = get_posts ( "order=ASC&orderby=rand&numberposts=$displayCount" );
	} else if ($num == 10) {
		$newposts = get_posts ( "orderby=modified&numberposts=$displayCount" );
	} else {
		$newposts = get_posts ( "orderby=id&numberposts=$displayCount" );
	}
	
	foreach ( $newposts as $post ) {
		$new_html = $siteHtml;
		
		//文章标题
		$new_html = str_ireplace ( "{article_title}", $post->post_title, $new_html );
		
		//文章简介
		$new_html = str_ireplace ( "{article_intro}", mb_strimwidth ( strip_tags ( apply_filters ( 'the_content', $post->post_content ) ), 0, 42, "…" ), $new_html );
		
		//文章uri
		$new_html = str_ireplace ( "{article_uri}", get_permalink ( $post->ID ), $new_html );
		
		//文章图片uri
		$new_html = str_ireplace ( "{article_img_uri}", get_post_meta ( $post->ID, "image_url", $single = true ), $new_html );
		
		$replacedHtml .= $new_html;
	}
	
	return $replacedHtml;

}

//文章点击排行榜（暂时采用随机生成）
function paihanglist($siteId, $lessstylecss, $morestylecss, $fennum) {
	global $wpdb;
	$replacedHtml = "";
	$displayCount;
	//数据库模板
	$siteHtml;
	//用于计数文章顺序编号
	$i = 0;
	
	$sql_site = "select shownum,site_html from  bw_template_site where id=$siteId";
	$result_site = $wpdb->get_results ( $sql_site );
	foreach ( $result_site as $row_site ) {
		$displayCount = $row_site->shownum;
		$siteHtml = $row_site->site_html;
	}
	
	$newposts = get_posts ( "orderby=rand&numberposts=$displayCount" );
	
	foreach ( $newposts as $post ) {
		
		$i += 1;
		
		$new_html = $siteHtml;
		
		//文章标题
		$new_html = str_ireplace ( "{article_title}", $post->post_title, $new_html );
		
		//文章简介
		$new_html = str_ireplace ( "{article_intro}", mb_strimwidth ( strip_tags ( apply_filters ( 'the_content', $post->post_content ) ), 0, 42, "…" ), $new_html );
		
		//文章uri
		$new_html = str_ireplace ( "{article_uri}", get_permalink ( $post->ID ), $new_html );
		
		//文章图片uri
		$new_html = str_ireplace ( "{article_img_uri}", get_post_meta ( $post->ID, "image_url", $single = true ), $new_html );
		
		if ($i <= $fennum) {
			$new_html = str_ireplace ( "{stylecss}", $lessstylecss, $new_html );
		} else {
			$new_html = str_ireplace ( "{stylecss}", $morestylecss, $new_html );
		}
		
		$replacedHtml .= $new_html;
	}
	
	return $replacedHtml;
}

/**
 * 
 * 根据分类ID取得该分类下的一定量的文章
 * @param unknown_type $siteId 位置id
 * @param unknown_type $catid 分类id
 * @param unknown_type $num
 * $sum ：数据库查询规则 
 * $num < 2 将按照文章发布日期查询orderby=post_date
 * $num == 3将按照文章的标题查询orderby=title
 * $num == 8数据库查询讲进行随机查询orderby=rand
 * $num等于其余数据，数据库查询将按照文章id进行查询
 * 
 */
function catid_article($siteId, $catid, $num) {
	global $wpdb;
	$replacedHtml = "";
	$displayCount;
	//数据库模板
	$siteHtml;
	
	$sql_site = "select shownum,site_html from  bw_template_site where id=$siteId";
	$result_site = $wpdb->get_results ( $sql_site );
	foreach ( $result_site as $row_site ) {
		$displayCount = $row_site->shownum;
		$siteHtml = $row_site->site_html;
	}
	if ($num < 2) {
		$newposts = get_posts ( "orderby=post_date&numberposts=$displayCount&category=$catid" );
	} else if ($num == 3) {
		$newposts = get_posts ( "orderby=title&numberposts=$displayCount&category=$catid&order=ASC" );
	} else if ($num == 8) {
		$newposts = get_posts ( "order=ASC&orderby=rand&numberposts=$displayCount&category=$catid" );
	} else if ($num == 10) {
		$newposts = get_posts ( "orderby=modified&numberposts=$displayCount&category=$catid" );
	} else {
		$newposts = get_posts ( "orderby=id&numberposts=$displayCount&category=$catid" );
	}
	
	foreach ( $newposts as $post ) {
		$new_html = $siteHtml;
		
		//文章标题
		$new_html = str_ireplace ( "{article_title}", $post->post_title, $new_html );
		
		//文章简介
		$new_html = str_ireplace ( "{article_intro}", mb_strimwidth ( strip_tags ( apply_filters ( 'the_content', $post->post_content ) ), 0, 42, "…" ), $new_html );
		
		//文章uri
		$new_html = str_ireplace ( "{article_uri}", get_permalink ( $post->ID ), $new_html );
		
		//文章图片uri
		$new_html = str_ireplace ( "{article_img_uri}", get_post_meta ( $post->ID, "image_url", $single = true ), $new_html );
		
		//文章发布日期
		$new_html = str_ireplace ( "{article_date}", date ( "Y-m-d", strtotime ( $post->post_date ) ), $new_html );
		
		$replacedHtml .= $new_html;
	}
	
	return $replacedHtml;
}

/**
 * 一：文章分类ID：
 * (1)服装搭配：3
 * (2)发型设计：4
 * (3)化妆：5
 * (4)养生：6
 * 
 * 二：自定义字段subcat_id:
 * 此subcat_id也就是文章的子分类的替身
 * 因为每个分类下的子分类都不尽相同
 * 所以此处自定义字段暂定义为1、2、3...格式
 * 
 * 根据自定义字段，文章分类查询文章，并显示
 * 1.发文章定义一个自定义字段subcat_id并赋值
 * 2.根据此自定义字段值，查询wp_postmeta表，得到文章ID
 * 3.根据文章ID，然后查询文章表，得到文章
 * 
 * 
 * 方法参数设置：
 * $site_id:文章所在网页显示位置id
 * $catid:文章分类id
 * $num:查询文章按某一顺序查询比如按时间、倒序、id，title等
 * $subcat_id:自定义字段值
 * 
 * 
 */
function subcatid($siteId, $catid, $num, $subcat_id) {
	global $wpdb;
	$replacedHtml = "";
	$displayCount;
	//数据库模板
	$siteHtml;
	
	$sql_site = "select shownum,site_html from  bw_template_site where id=$siteId";
	$result_site = $wpdb->get_results ( $sql_site );
	
	$args = array ('category' => $catid, 'numberposts' => $displayCount, 'orderby' => 'rand', 'order' => 'DESC', 'meta_key' => 'subcat_id', 'meta_value' => $subcat_id );
	foreach ( $result_site as $row_site ) {
		$displayCount = $row_site->shownum;
		$siteHtml = $row_site->site_html;
	}
	if ($num < 2) {
		$newposts = get_posts ( "orderby=post_date&numberposts=$displayCount&category=$catid&metakey=subcat_id&meta_value=$subcat_id" );
	} else if ($num == 3) {
		$newposts = get_posts ( "orderby=title&numberposts=$displayCount&category=$catid&order=ASC&metakey=subcat_id&meta_value=$subcat_id" );
	} else if ($num == 8) {
		$newposts = get_posts ( $args );
	} else {
		$newposts = get_posts ( "orderby=id&numberposts=$displayCount&category=$catid&metakey=subcat_id&meta_value=$subcat_id&order=ASC" );
	}
	
	foreach ( $newposts as $post ) {
		$new_html = $siteHtml;
		
		//文章标题
		$new_html = str_ireplace ( "{article_title}", $post->post_title, $new_html );
		
		//文章简介
		$new_html = str_ireplace ( "{article_intro}", mb_strimwidth ( strip_tags ( apply_filters ( 'the_content', $post->post_content ) ), 0, 42, "…" ), $new_html );
		
		//文章uri
		$new_html = str_ireplace ( "{article_uri}", get_permalink ( $post->ID ), $new_html );
		
		//文章图片uri
		$new_html = str_ireplace ( "{article_img_uri}", get_post_meta ( $post->ID, "image_url", $single = true ), $new_html );
		
		//文章发布日期
		$new_html = str_ireplace ( "{article_date}", date ( "Y-m-d", strtotime ( $post->post_date ) ), $new_html );
		
		$replacedHtml .= $new_html;
	}
	
	return $replacedHtml;
}